package com.dxx.cloud.admin.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author duh
 * @create 2019/1/9 10:03

 **/
public class DateUtils {
    private static final String timeFormat = "yyyy-MM-dd HH:mm:ss";
    private static final String timeFormat2 = "yyyyMMddHHmmss";
    private static final String dateFormat = "yyyy-MM-dd";
    private static final SimpleDateFormat timeSdf = new SimpleDateFormat(timeFormat);
    private static final SimpleDateFormat timeSdf2 = new SimpleDateFormat(timeFormat2);
    private static final SimpleDateFormat dateSdf = new SimpleDateFormat(dateFormat);
    private static final String DAY_END_TIME = "23:59:59";

    public static Date parseTime(String timeString){
        Date date = null;
        try {
            date = timeSdf.parse(timeString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    public static Date parseDate(String dateString){
        Date date = null;
        try {
            date = dateSdf.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static Date parse(String dateOrTimeStr){
        if(null == dateOrTimeStr || "".equals(dateOrTimeStr)){
            return null;
        }
        if(dateOrTimeStr.length()==dateFormat.length()){
            return parseDate(dateOrTimeStr);
        }else if(dateOrTimeStr.length()==timeFormat.length()){
            return parseTime(dateOrTimeStr);
        }else {
            return null;
        }
    }
    public static Date parseDayEnd(String dateOrTimeStr){
        if(null == dateOrTimeStr || "".equals(dateOrTimeStr)){
            return null;
        }
        if(dateOrTimeStr.length()==dateFormat.length()){
            return parseTime(dateOrTimeStr+" "+DAY_END_TIME);
        }else if(dateOrTimeStr.length()==timeFormat.length()){
            return parseTime(dateOrTimeStr);
        }else {
            return null;
        }
    }
    public static String currentTimeString(){
        return timeSdf.format(new Date());
    }
    public static String currentTimeString2(){
        return timeSdf2.format(new Date());
    }

    public static String data2String(Date date) {
        return timeSdf.format(date);
    }
}
